package 用栈实现队列;

import java.util.Stack;

/**
 * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）：
 */
public class MyQueue {

    private Stack<Integer> stackIn;
    private Stack<Integer> stackOut;

    public MyQueue() {
        this.stackIn = new Stack<Integer>();
        this.stackOut = new Stack<Integer>();
    }

    /**
     * void push(int x) 将元素 x 推到队列的末尾
     */
    public void push(int x) {
        stackIn.push(x);
    }

    /**
     * int pop() 从队列的开头移除并返回元素
     *
     * @return
     */
    public int pop() {
        dumpstackIn();
        return stackOut.pop();
    }

    /**
     * int peek() 返回队列开头的元素
     *
     * @return
     */
    public int peek() {
        dumpstackIn();
        return stackOut.peek();
    }

    /**
     * boolean empty() 如果队列为空，返回 true ；否则，返回 false
     *
     * @return
     */
    public boolean empty() {
        dumpstackIn();
        if( stackOut.isEmpty() )
            return true;
        else
            return false;
    }

    private void dumpstackIn() {
        if (!stackOut.isEmpty()) {
            return;
        }
        while (!stackIn.isEmpty()) {
            Integer i = stackIn.pop();
            stackOut.push(i);
        }
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */